package models

import (
	"io"
	"net"

	mqtt "github.com/eclipse/paho.mqtt.golang"
	"openzbox.com/protocol/modbus"
)

type DeviceInfo struct {
	ID                       int64              `gorm:"not null;column:ID; PRIMARY_KEY"`
	DeviceInfoID             string             `gorm:"not null;column:DeviceInfoID"` //设备id
	Name                     string             `gorm:"not null;column:Name"`
	Describe                 *string            `gorm:"column:Describe"`
	TransportLayerProtocol   int64              `gorm:"column:TransportLayerProtocol"`   //传输层协议1:tcp
	ApplicationLayerProtocol int64              `gorm:"column:ApplicationLayerProtocol"` //应用层协议1:slyun 2:dtu 3:mqtts_广东水利物联网平台 4:mqtts_onenet 5:ModbusTCPServer 6:四联四表
	IP                       string             `gorm:"not null;column:IP"`
	PORT                     string             `gorm:"not null;column:PORT"`
	OHRT                     int64              `gorm:"not null;column:OHRT"`                     //心跳 秒
	SPITV                    int64              `gorm:"not null;column:SPITV;default:0"`          //上传周期 分
	OHRTCode                 *string            `gorm:"column:OHRTCode;default:NULL"`             //心跳命令码 dtu专用
	ComName                  *string            `gorm:"column:ComName;default:NULL"`              //串口名称
	BAD                      *uint              `gorm:"column:BAD;default:NULL"`                  //波特率
	DAB                      *uint              `gorm:"column:DAB;default:NULL"`                  //数据位
	CHC                      *int               `gorm:"column:CHC;default:NULL"`                  // PARITY_NONE = 0	PARITY_ODD= 1	 PARITY_EVEN = 2
	STO                      *uint              `gorm:"column:STO;default:NULL"`                  //停止位
	Conn                     net.Conn           `gorm:"-" `                                       //四联云平台连接状态
	DtuReadWriteCloser       io.ReadWriteCloser `gorm:"-" `                                       //dtu透传类
	MQTTClient               *mqtt.Client       `gorm:"-" `                                       //mqtts_广东水利物联网平台的连接状态
	MQTTHostName             *string            `gorm:"column:MQTTHostName;"`                     //mqtt：域名orIP
	MQTTPort                 *string            `gorm:"column:MQTTPort;"`                         //mqtt：端口
	MQTTClientID             *string            `gorm:"column:MQTTClientID;"`                     //mqtt：ClientID
	MQTTUsername             *string            `gorm:"column:MQTTUsername;"`                     //mqtt：Username
	MQTTAccessKey            *string            `gorm:"column:MQTTAccessKey;"`                    //mqtts_广东水利物联网平台：AccessKey
	MQTTSubscribe            *string            `gorm:"column:MQTTSubscribe;"`                    //mqtt 订阅路径
	MQTTPublish              *string            `gorm:"column:MQTTPublish;"`                      //mqtt 发布路径
	IsReportedData           int                `gorm:"not null;column:IsReportedData;default:1"` //是否上报 1:上报 2：不上报

	ModbusTCPClient              []NodeRegisterList             `gorm:"-" `                 //modubs连接状态
	ModbusTCPIP                  *string                        `gorm:"column:ModbusTCPIP"` //modbustcp上行协议
	ModbusTCPPORT                *string                        `gorm:"column:ModbusTCPPORT"`
	DeviceInfo_Concentrator      []DeviceInfo_Concentrator      `gorm:"foreignKey:Device_ID;references:ID;constraint:OnDelete:CASCADE"`
	DeviceInfo_Concentrator_User []DeviceInfo_Concentrator_User `gorm:"foreignKey:Device_ID;references:ID;constraint:OnDelete:CASCADE"`
	DeviceInfo_Detail            []DeviceInfo_Detail            `gorm:"foreignKey:Device_ID;references:ID;constraint:OnDelete:CASCADE"`
	CollectData                  []CollectData                  `gorm:"foreignKey:Device_ID;references:ID;constraint:OnDelete:CASCADE"`
}

// 配置数据库操作的表名称
func (DeviceInfo) TableName() string {
	return "DeviceInfo"
}

type EditDeviceInfoRequest struct {
	DeviceInfo DeviceInfo `json:"DeviceInfo"`
}

// 对应的不同地址位连接
type NodeRegisterList struct {
	SlaveID      string
	NodeRegister *modbus.NodeRegister
}
